package com.ChenYaRou.www.controller;

import com.ChenYaRou.www.constant.JsonBeanConstant;
import com.ChenYaRou.www.dao.MessageDao;
import com.ChenYaRou.www.dao.UserDao;
import com.ChenYaRou.www.dao.impl.MessageDaoImpl;
import com.ChenYaRou.www.dao.impl.UserDaoImpl;
import com.ChenYaRou.www.po.*;
import com.ChenYaRou.www.utils.GenerateSql;
import com.ChenYaRou.www.utils.JsonBean;
import com.ChenYaRou.www.utils.UpdateOrInsertOrSelectMoreAllBean;

import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.ChenYaRou.www.constant.Constant.*;
import static com.ChenYaRou.www.constant.False.EMPTY;

/**
 * @ClassName GetNoticeServletController
 * @Description TODO
 * @Author 86183
 * @Date 2022/5/1 17:01
 * @Version 1.0
 **/
public class NoticeServletController {
    /**
     * 获取notice通知
     * @param role
     * @return
     */
    public String getNotice(Role role){
        if(role!=null){
            Notice notice=new Notice();
            if(MANAGER.equals(role.getRole())){
                notice.setState(true);
            }else{
                notice.setRecipientId(role.getId());
            }
            List<JsonBeanNotice> jsonBeanNotices=new ArrayList<>();
            List<Notice> notices = UpdateOrInsertOrSelectMoreAllBean.getInstance().selectMore(notice);
            if(notices!=null && !notices.isEmpty()){
                for (Notice notice1 : notices) {
                    UserDao userDao=new UserDaoImpl();
                    try {
                        User user = userDao.select(notice1.getRecipientId());
                        jsonBeanNotices.add(
                                new JsonBeanNotice(notice1.getId(), notice1.getContent(),notice1.getRecipientId(),
                                        user.getName(), notice1.getUpdateTime(),notice1.getState()));
                    } catch (IllegalAccessException | SQLException | NoSuchFieldException | InstantiationException e) {
                        e.printStackTrace();
                    }
                }
            }
            return JsonBean.operateList(JsonBeanConstant.NULL,jsonBeanNotices);
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 可多条notice一并操作 新追加的notice集合容量必须与旧notice集合一一对应 --即管理员处理投诉必须拥有回应
     * 通过notice封禁和解禁时newObject和oldObject不能为空
     * 管理员查阅通知(state-false)并发送新通知
     * @param newNotice 管理员在处理notice时同时发送新notice告知用户
     * @param oldNotice 原先的notice,将其state设置为false 即管理员不需要再次查阅
     * @param newObject 管理员在封禁 解禁用户和商家店铺时需要设置两个object 否则 不需要
     * @param oldObject
     * @return
     */
    public <K> String updateNotice(List<Notice> newNotice,List<Notice> oldNotice,K newObject,K oldObject){
        if(newNotice!=null && oldNotice!=null && !newNotice.isEmpty() && !oldNotice.isEmpty()){
            for(int i=0;i<oldNotice.size();i++){
                Notice updateOldNotice=new Notice();
                updateOldNotice.setState(false);
                try {
                    String sqlInsertNewNotice= GenerateSql.getInstance().insert(newNotice.get(i));
                    String sqlUpdateOldNotice = GenerateSql.getInstance().update(updateOldNotice,oldNotice.get(i));
                    String closeOrActive=null;
                    if(newObject!=null && oldObject!=null){
                        closeOrActive=GenerateSql.getInstance().update(newObject,oldObject);
                    }
                    return UpdateOrInsertOrSelectMoreAllBean.getInstance().insertOrUpdateMore(
                            sqlInsertNewNotice,sqlUpdateOldNotice,closeOrActive);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 商家用户投诉的notice信息设置
     * @param notice
     * @return
     */
    public String addNotice(Notice notice){
        return UpdateOrInsertOrSelectMoreAllBean.getInstance().insert(notice);
    }

    /**
     * 管理员直接封禁解禁并通知用户/商家
     * @param notice
     * @param newObject
     * @param oldObject
     * @param <K>
     * @return
     */
    public <K>String closeOrActive(Notice notice,K newObject,K oldObject){
        if(notice!=null && newObject!=null && oldObject!=null){
            try {
                String sqlUpdate=GenerateSql.getInstance().update(newObject,oldObject);
                String sqlSendNotice=GenerateSql.getInstance().insert(notice);
                return UpdateOrInsertOrSelectMoreAllBean.getInstance().insertOrUpdateMore(sqlUpdate,sqlSendNotice);
            } catch (IllegalAccessException  e) {
                e.printStackTrace();
            }
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 点赞通知和评论赞+1
     * @param content
     * @param state
     * @param recipientId
     * @param messageId
     * @return
     */
    public String like(String content,Boolean state,Integer recipientId,Integer messageId){
        if(content!=null && recipientId!=null && messageId!=null){
            try {
                MessageDao messageDao=new MessageDaoImpl();
                Message message=new Message();
                message.setId(messageId);
                List<Message> messages = messageDao.selectMore(message);
                if(messages!=null && !messages.isEmpty()){
                    Message newMessage=new Message();
                    newMessage.setLikes(messages.get(0).getLikes()+1);
                    String updateMessageLikes=GenerateSql.getInstance().update(newMessage,messages.get(0));
                    Notice notice=new Notice(content,LocalDateTime.now(),state,recipientId);
                    String insertNotice=GenerateSql.getInstance().insert(notice);
                    return UpdateOrInsertOrSelectMoreAllBean.getInstance().insertOrUpdateMore(
                            updateMessageLikes, insertNotice);
                }
            } catch (IllegalAccessException | SQLException | NoSuchFieldException | InstantiationException e) {
                e.printStackTrace();
            }
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 给评论回复评论
     * @param content
     * @param state
     * @param recipientId
     * @param parentId
     * @param shopId
     * @param reContent
     * @param userId
     * @return
     */
    public String reContent(String content,boolean state,Integer recipientId, Integer parentId,
                             Integer shopId,String reContent,Integer userId){
        if(content!=null && recipientId!=null &&shopId!=null && reContent!=null && userId!=null){
            Notice notice =new Notice(content, LocalDateTime.now(),state,recipientId);
            Message message=new Message(shopId,reContent,userId,LocalDateTime.now(),0,parentId,SHOW);
            try {
                String sqlInsertNotice=GenerateSql.getInstance().insert(notice);
                String sqlInsertMessage=GenerateSql.getInstance().insert(message);
                return UpdateOrInsertOrSelectMoreAllBean.getInstance().insertOrUpdateMore(
                        sqlInsertNotice, sqlInsertMessage);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 管理员通过创建店铺的申请
     * @param content
     * @param oldNoticeId
     * @param recipientId
     * @return
     */
    public String passCreatNewShop(String content,Integer oldNoticeId,Integer recipientId){
        if(content!=null && oldNoticeId!=null && recipientId!=null){
            Notice newNotice=new Notice(content,LocalDateTime.now(),false,recipientId);

            Notice updateOldNotice=new Notice();
            updateOldNotice.setState(false);

            Notice oldNotice=new Notice();
            oldNotice.setId(oldNoticeId);

            Shop shopOld=new Shop();
            shopOld.setOwnerId(recipientId);

            Shop shopNew=new Shop();
            shopNew.setState(OPEN);

            try {
                String updateNotice = GenerateSql.getInstance().update(updateOldNotice,oldNotice);
                String insertNotice = GenerateSql.getInstance().insert(newNotice);
                String updateShop = GenerateSql.getInstance().update(shopNew,shopOld);
                return UpdateOrInsertOrSelectMoreAllBean.getInstance().insertOrUpdateMore(
                        updateNotice, insertNotice, updateShop);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }

    /**
     * 被封禁时的用户投诉
     * @param notice
     * @param userName
     * @return
     */
    public String complaint(Notice notice,String userName){
        if(notice!=null && userName!=null &&!EMPTY.equals(userName.trim())){
            UserDao userDao=new UserDaoImpl();
            try {
                User user = userDao.select(userName);
                notice.setRecipientId(user.getId());
                return addNotice(notice);
            } catch (IllegalAccessException | SQLException | NoSuchFieldException | InstantiationException e) {
                e.printStackTrace();
            }
        }
        return JsonBean.operateList(JsonBeanConstant.FRONT_ERROR,null);
    }
}
